package assignments.sagarjoshi.springtms;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class AddorEditCourse extends Activity {

    EditText eCourseName, eDescription, eTrainerName, ePlace;
    String sCourseName, sDescription, sTrainerName, sPlace;
    Button bSave, bReset;
    int val;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_or_edit_course);

        ActionBar b = getActionBar();
        b.setTitle("New/Edit Course");
        b.show();

        Intent intent = getIntent();
        val=intent.getIntExtra("item_id",0);
        Log.i("value for intent", String.valueOf(val));

        eCourseName = (EditText) findViewById(R.id.editText7);
        eDescription = (EditText) findViewById(R.id.editText8);
        eTrainerName = (EditText) findViewById(R.id.editText9);
        ePlace = (EditText) findViewById(R.id.editText10);

        bSave = (Button) findViewById(R.id.button3);
        bReset = (Button) findViewById(R.id.button4);

        if(val>0) {
            MyDatabaseHelper myDatabaseHelper = new MyDatabaseHelper(getApplicationContext());
            Cursor cursor = myDatabaseHelper.EditCourse(val);

            eCourseName.setText(cursor.getString(1));
            eDescription.setText(cursor.getString(2));
            ePlace.setText(cursor.getString(3));
            eTrainerName.setText(cursor.getString(4));
        }
        final Context context = this;
        bSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                sCourseName = eCourseName.getText().toString();
                sDescription = eDescription.getText().toString();
                sTrainerName = eTrainerName.getText().toString();
                sPlace = ePlace.getText().toString();

                if (val==0){
                    if (sCourseName.isEmpty() || sDescription.isEmpty() || sTrainerName.isEmpty() || sPlace.isEmpty()) {
                        Toast.makeText(getApplicationContext(), "Enter all fields...", Toast.LENGTH_LONG).show();
                    } else {
                        Course course = new Course(sCourseName, sDescription, sTrainerName, sPlace);
                        MyDatabaseHelper myDatabaseHelper = new MyDatabaseHelper(getApplicationContext());

                        long result = myDatabaseHelper.AddCourse(course);
                        if (result == 0) {
                            Toast.makeText(getApplicationContext(), "Records not added...", Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(getApplicationContext(), "Record Added successfully...!!!", Toast.LENGTH_LONG).show();
                            eCourseName.setText("");
                            eDescription.setText("");
                            eTrainerName.setText("");
                            ePlace.setText("");
                            ((Activity) context).finish();
                        }
                    }
                }
                else{
                    //eCourseName.setEnabled(false);
                    MyDatabaseHelper myDatabaseHelper = new MyDatabaseHelper(getApplicationContext());
                    Course course = new Course(val,sCourseName, sDescription, sTrainerName, sPlace);
                    int res=myDatabaseHelper.UpdateCourse(course);
                    if(res>0){
                        Toast.makeText(getApplicationContext(),"Record updated successfully...",Toast.LENGTH_LONG).show();
                        ((Activity) context).finish();
                    }
                }
            }
        });

        bReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                eCourseName.setText("");
                eDescription.setText("");
                eTrainerName.setText("");
                ePlace.setText("");
            }
        });

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_addor_edit_course, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
